<template>
  <div>
    <el-form ref="form" :model="FEditClass" label-width="80px">
      <el-form-item label="组织类型">
        <el-input v-model="FEditClass.FClassName"></el-input>
      </el-form-item>
      <el-form-item label="上级结构">
        <el-cascader
          v-model="FEditClass.tempPClassID"
          :options="FElmDataTree"
          :props="{ checkStrictly: true }"
          filterable
          clearable
        ></el-cascader>
      </el-form-item>
      <!--集团, 公司， 部门， 团队, 自定义 -->
      <el-form-item label="标准类型">
        <el-select v-model="FEditClass.FClassType" placeholder="请选择">
          <el-option
            v-for="item in FOptionsTypes"
            :key="item"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-row justify="end">
      <el-button type="primary" @click="onSave" :disabled="!zIsOpenData"
        >保存</el-button
      >
      <el-button type="warning" @click="onCancel">取消</el-button>
    </el-row>
  </div>
</template>

<script  lang="ts">
import { TBaseCompanyClass } from "@/baselib/right/BaseRightClass";
import { Options, prop, Vue } from "vue-class-component";
import { Prop } from "vue-property-decorator";
import TFormBase from "@/views/BaseForm";
import elmHelper, { IElmTreeData } from "../../model/elmHelper";
//配合着用
@Options({
  name: "TFormCompanyClassEdit",
  components: {},
  computed: {},
  data() {},
})
export default class TFormCompanyClassEdit extends TFormBase {
  private FOptionsTypes: any[] = [
    "自定义",
    "集团",
    "公司",
    "部门",
    "团队",
  ];
  private FEditClass: TBaseCompanyClass = new TBaseCompanyClass();
  private FClassList: TBaseCompanyClass[] = [];
  private FElmDataTree: IElmTreeData[] = [];
  public created() {
    this.FEditClass = this.zContextParams.IndexClass;
    this.FEditClass.tempPClassID = this.FEditClass.FPClassID;
    this.FClassList = this.zContextParams.ClassList;
    //转树形数据
    this.FElmDataTree = elmHelper.dataTreeToElmTree(
      this.FClassList,
      "FClassID",
      "children",
      "FClassName",
      "FClassID"
    );
    if (!this.$myStrHelper.stringIsEmpty(this.FEditClass.FClassID)) {
      //this.FEditCaption = "结构类型编辑";
    }
    this.openData();
  }
  //打开数据
  public async openData() {
    if (!this.$myStrHelper.stringIsEmpty(this.FEditClass.FClassID)) {
      //打开数据
      this.zIsOpenData = false;
      let lResult = await this.$myBaseRightAPI.getCompanyClass(
        this.FEditClass.FClassID
      );
      if (!lResult.resultSuccess) {
        this.$myMsgHelp.msgDialog(lResult.resultMsg);
        return;
      }
      this.FEditClass = lResult.resultData;
      if (this.$myStrHelper.stringIsEmptyYW(this.FEditClass.FClassID)) {
        this.$myMsgHelp.msgDialog(
          "返回的数据结构有问题:" + JSON.stringify(lResult.resultData)
        );
        return;
      }
      this.FEditClass.tempPClassID = this.FEditClass.FPClassID;
      this.zIsOpenData = true;
    } else {
      this.zIsOpenData = true;
    }
  }
  //
  private async onSave() {
    //当清空是elm返回的是null,多级返回的是数组
    if (this.FEditClass.tempPClassID == null) {
      this.FEditClass.FPClassID = "";
    } else if (this.FEditClass.tempPClassID instanceof Array) {
      let temp =
        this.FEditClass.tempPClassID[this.FEditClass.tempPClassID.length - 1];
      if (this.FEditClass.FClassID == temp) {
        this.FEditClass.tempPClassID = this.FEditClass.FPClassID;
      } else {
        this.FEditClass.FPClassID = temp;
      }
    }
    //保存数据
    let lResult = await this.$myBaseRightAPI.saveCompanyClass(this.FEditClass);
    if (!lResult.resultSuccess) {
      this.$myMsgHelp.msgDialog(lResult.resultMsg);
      return;
    }
    this.$myMsgHelp.msgHintAutoClose(
      "保存数据成功",
      this.$myMsgHelp.msgSuccess
    );
    if (this.zContextOnCall != null && this.zContextOnCall != undefined) {
      this.zContextOnCall("save", this.FEditClass);
    }
  }
  private onCancel() {
    if (this.zContextOnCall != null && this.zContextOnCall != undefined) {
      this.zContextOnCall("close", this.FEditClass);
    }
  }
  
}
</script>